package org.funtester.performance.books.chapter04.section3;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.funtester.performance.books.chapter04.section2.HttpClientTool;
import org.funtester.performance.books.chapter04.section2.HttpRequestTool;

import java.io.IOException;

public class UserBase {

    /**
     * HttpClient对象
     */
    public static CloseableHttpClient httpClient = HttpClientTool.getHttpClient();// 获取HttpClient对象

    public static String HOST = "http://localhost:8080";

    /**
     * 用户名
     */
    public String username;

    /**
     * 用户密码
     */
    public String password;

    /**
     * 用户登录凭证token
     */
    public String token;

    /**
     * 构造函数,设置用户名和密码
     * @param username
     * @param password
     */
    public UserBase(String username, String password) {
        this.username = username;// 设置用户名
        this.password = password;// 设置用户密码
    }

    /**
     * 构造函数,设置用户登录凭证token
     * @param token
     */
    public UserBase(String token) {
        this.token = token;// 设置用户登录凭证token
    }

    public UserBase(UserBase base) {
        this.username = base.username;// 设置用户名
        this.password = base.password;// 设置用户密码
        this.token = base.token;// 设置用户登录凭证token
    }

    /**
     * 用户登录
     */
    public void login() {
        String url = "/login";// 登录接口
        JSONObject params = new JSONObject();// 创建请求参数 JSON
        params.put("username", username);// 设置用户名
        params.put("password", password);// 设置用户密码
        JSONObject login = getPostResponse(url, params);// 登录
        token = login.getString("token");// 获取登录凭证token
    }

    /**
     * 获取公共请求参数
     * @return
     */
    public JSONObject getParams() {
        JSONObject params = new JSONObject();
        params.put("timestamp", System.currentTimeMillis());// 设置时间戳参数
        return params;
    }

    /**
     * 获取GET请求响应
     * @param url
     * @return
     */
    public JSONObject getGetResponse(String url) {
        HttpGet httpGet = HttpRequestTool.getHttpGet(HOST + url, null);// 获取HttpGet对象
        return getResponse(httpGet);// 获取响应
    }

    /**
     * 获取GET请求响应
     * @param url
     * @param params
     * @return
     */
    public JSONObject getGetResponse(String url, JSONObject params) {
        HttpGet httpGet = HttpRequestTool.getHttpGet(HOST + url, params);// 获取HttpGet对象
        return getResponse(httpGet);// 获取响应
    }

    /**
     * 获取POST请求响应
     * @param url
     * @return
     */
    public JSONObject getPostResponse(String url) {
        HttpPost httpPost = HttpRequestTool.getHttpPost(HOST + url, "");// 获取HttpGet对象
        return getResponse(httpPost);// 获取响应
    }

    /**
     * 获取POST请求响应
     * @param url
     * @param params
     * @return
     */
    public JSONObject getPostResponse(String url, JSONObject params) {
        HttpPost httpPost = HttpRequestTool.getHttpPost(HOST + url, params);// 获取HttpGet对象
        return getResponse(httpPost);// 获取响应
    }

    /**
     * 获取响应
     * @param requestBase
     * @return
     */
    public JSONObject getResponse(HttpRequestBase requestBase) {
        requestBase.addHeader("token", token);// 设置token
        try (CloseableHttpResponse response = httpClient.execute(requestBase)) {// 执行请求
            int statusCode = response.getStatusLine().getStatusCode();// 获取响应状态码
            JSONObject reponseJson = HttpRequestTool.parseReponseJson(response);// 解析响应 JSON
            if (statusCode == HttpStatus.SC_OK && reponseJson.getInteger("code") == 0) {// 判断响应状态码和响应code
                return reponseJson;// 请求成功,返回响应 JSON
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;// 请求失败,返回null
    }


}
